﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AssetsSys.BLL.AutoMapper;
using AssetsSys.DAL;
using AssetsSys.Dto;
using AssetsSys.Dto.Assets;
using AssetsSys.Dto.Status;
using AssetsSys.model;

namespace AssetsSys.BLL
{
    /// <summary>
    /// 资产状态
    /// </summary>
    public class StatusManger
    {
        /// <summary>
        /// 获取状况列表
        /// </summary>
        /// <returns></returns>
        public static async Task<List<StatusModel>> GetList()
        {
            using (var staSvc = new StatusService())
            {
                var status = await staSvc.GetAll().OrderByDescending(x => x.CreateTime).ToListAsync();
                return status.MapTo<List<StatusModel>>();
            }
        }

        /// <summary>
        /// 添加状况
        /// </summary>
        /// <param name="statusName"></param>
        /// <returns></returns>
        public static async Task<bool> Create(string statusName)
        {
            using (var staSvc = new StatusService())
            {
                await staSvc.CreateAsync(new Status()
                {
                    StatusName = statusName
                });
                return true;
            }
        }

        /// <summary>
        /// 删除状况
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<bool> Delete(Guid id)
        {
            using (var staSvc = new StatusService())
            {
                var model = await staSvc.GetOne(id);
                if (model == null)
                {
                    return false;
                }
                await staSvc.Remove(id);
                return true;
            }
        }

        /// <summary>
        /// 获取状况信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<StatusModel> GetById(Guid id)
        {
            using (var staSvc = new StatusService())
            {
                return (await staSvc.GetOne(id)).MapTo<StatusModel>();
            }
        }

        /// <summary>
        /// 变更状况
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static async Task<bool> Update(StatusModel model)
        {
            using (var staSvc = new StatusService())
            {
                var oldModel = await staSvc.GetOne(model.Id);
                if (oldModel == null)
                {
                    return false;
                }
                oldModel.StatusName = model.StatusName ?? oldModel.StatusName;
                await staSvc.EditAsvnc(oldModel);
            }
            return true;
        }

        public static async Task<List<CountModel>> GetCountInfo()
        {
            var countNum = new List<CountModel>();
            using (var staSvc = new StatusService())
            {
                //获取情况信息
                var status = await staSvc.GetAll().OrderByDescending(x => x.CreateTime).ToListAsync();
                using (var assSvc = new AssetsService())
                {
                    var assetsList = await assSvc.GetAll().Select(x => new AssetsResponseModel
                    {
                        Id = x.Id,
                        AssetsNum = x.AssetsNum,
                        AssetsName = x.AssetsName,
                        AssetsDescribe = x.AssetsDescribe,
                        Decimal = x.Decimal,
                        Type = x.Type.TypeName,
                        AgeLimit = x.Type.AgeLimit,
                        Source = x.Source.SourceName,
                        Status = x.Status.StatusName,
                        PrincipalUser = x.PrincipalUser.UserName,
                        PrincipalDepartment = x.PrincipalUser.Department.DepartmentName,
                        StorageUser = x.StorageUser.UserName,
                        StorageDepartment = x.StorageUser.Department.DepartmentName,
                        CreateTime = x.CreateTime
                    }).ToListAsync();
                    status.ForEach(x=>
                    {
                       var name = x.StatusName;
                        var count = "";
                        var timestatus = new List<string>()
                        {
                            "资产处置",
                            "遗失"
                        };
                        //只展示一年内的故障
                        count = timestatus.Contains(x.StatusName) ?
                        assetsList.Count(y => y.Status == x.StatusName&& y.CreateTime.AddYears(1) > DateTime.Now).ToString() : 
                        assetsList.Count(y => y.Status == x.StatusName).ToString();
                       
                        countNum.Add(new CountModel()
                        {
                            CountName = name,
                            CountNum = count,
                            Units = "件"
                        });
                    });
                    return countNum;
                }
            }
        }
    }
}
